#include <bits/stdc++.h>

#include <atcoder/all>
#define LL long long
using namespace std;
using namespace atcoder;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;

  for (cin >> T; T; T--) {
    int n, m;

    cin >> n >> m;
    vector<vector<int>> ans(n, vector<int>(m));
    vector<bool> used(n * m + 1, false);
    vector<int> X(n), Y(m);

    for (int i = 0; i < n; i++) {
      cin >> X[i];
    }
    for (int j = 0; j < m; j++) {
      cin >> Y[j];
    }

    bool succ = true;
    map<int, int> mp1, mp2;
    for (int i = 0; i < n; i++) {
      if (mp1.count(X[i])) {
        succ = false;
      }
      mp1[X[i]] = i;
    }
    for (int j = 0; j < m; j++) {
      if (mp2.count(Y[j])) {
        succ = false;
      }
      mp2[Y[j]] = j;
    }

    if (succ) {
      for (int i = 0; i < n; i++) {
        if (mp2.count(X[i])) {
          ans[i][mp2[X[i]]] = X[i];
          used[X[i]] = true;
        }
      }

      vector<set<pair<int, int>>> minv(n * m + 1);

      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (ans[i][j] == 0) {
            minv[min(X[i], Y[j])].insert(make_pair(i, j));
          }
        }
      }

      for (int num = n * m, t = n * m; num && succ; num--) {
        if (used[num]) {
          continue;
        }
        for (; minv[t].empty(); t--);

        if (num > t) {
          succ = false;
          break;
        }
        if (mp1.count(num)) {
          int i = mp1[num];

          succ = false;
          for (int j = 0; j < m; j++) {
            if (ans[i][j] == 0 && Y[j] > num) {
              ans[i][j] = num;
              minv[num].erase(make_pair(i, j));
              succ = true;
              break;
            }
          }
        } else if (mp2.count(num)) {
          int j = mp2[num];

          succ = false;
          for (int i = 0; i < n; i++) {
            if (ans[i][j] == 0 && X[i] > num) {
              ans[i][j] = num;
              minv[num].erase(make_pair(i, j));
              succ = true;
              break;
            }
          }
        } else {
          int i = minv[t].begin()->first, j = minv[t].begin()->second;

          ans[i][j] = num;
          minv[t].erase(minv[t].begin());
        }
      }
    }
    if (succ) {
      cout << "Yes\n";
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          cout << ans[i][j] << " ";
        }
        cout << "\n";
      }
    } else {
      cout << "No\n";
    }
  }
  return 0;
}